package top.xzxsrq.datahelp.zhixin.module.socket.handler;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
import top.xzxsrq.datahelp.zhixin.ZhixinApplication;
import top.xzxsrq.datahelp.zhixin.module.socket.entity.MessageTypeEntity;
import top.xzxsrq.datahelp.zhixin.module.socket.service.WebSocketService;
import top.xzxsrq.datahelp.zhixin.myenum.MessageTypeEnum;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: datahelp
 * @create: 2021-11-30 09:25
 **/
@Component
public class DataTypeHandler implements SocketMessageBaseHandler {

    @Autowired
    private ZhixinApplication zhixinApplication;

    @Autowired
    private WebSocketService webSocketService;

    @Override
    public void sockerGetMessage(MessageTypeEntity messageTypeEntity, WebSocketSession session) throws IOException {
        if (MessageTypeEnum.DATA_TYPE.getType().equals(messageTypeEntity.getMessageType())) {
            zhixinApplication.setDataType(messageTypeEntity.getValue());
            List<String> notIds = new LinkedList<>();
            notIds.add(session.getId());
            webSocketService.broadcastMsgNotId(JSON.toJSONString(messageTypeEntity), notIds);
        }
    }
}
